﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;

namespace CNdotnet.Ezsocio.ContactService.Email {
    public class EmailCnYahooService:IEmailService {
        #region Fields

        #endregion

        #region Constructors

        #endregion

        #region Properties
        public virtual string EmailSystem {
            get { return "yahoo.cn"; }
        }

        public  string StartUrl {
            get { return "http://mail.cn.yahoo.com/"; }
        }

        public Encoding DefaultEncoding {
            get { return Encoding.GetEncoding("gb2312"); }
        }
        #endregion

        #region Events

        #endregion       

        #region Methods
        public MailContractCollection GetContracts(string userName, string pwd) {
            MailContractCollection mc = new MailContractCollection();
            CookieContainer cc = new CookieContainer();
            string accept = "image/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight-2-b2, application/x-silverlight, */*";
            string language = "zh-cn";
            string refer = "http://mail.cn.yahoo.com/";
            string contentType = "application/x-www-form-urlencoded";
            string returnUrl = StartUrl;
            string html = WebOpHelper.GetHtml(StartUrl, accept, contentType, refer, language, cc, DefaultEncoding, out returnUrl);
            string _remember = WebOpHelper.Resove(html,"name=\".remember\" value=\"", "\"");
            string _done = WebOpHelper.Resove(html, "name=.done value=\"", "\"");
            string d = Rewrite(_remember, _done);
            string challenge = WebOpHelper.Resove(html, "name=\".challenge\" value=\"", "\"");
            var cookies = new NameValueCollection();
            cookies.Add("cn_challenge", challenge);
            cookies.Add("domain", new Uri(StartUrl).Host);
            DateTime dt = DateTime.Now.AddDays(100);
            cookies.Add("expires", dt.ToString());
            string ck = WebOpHelper.GetNameValueRowData(cookies, ',');            
            string h = Hash(pwd, challenge);
            string nextUrl = string.Format("http://edit.bjs.yahoo.com/config/login?.intl=cn&.done={3}&.src=ym&.cnrid=ymhp_20000&.challenge={0}&login={1}@{4}&passwd={2}&.remember=y&.persistent=&submit=&.hash=1&.js=1&.md5=1", challenge, userName, h, d,this.EmailSystem);
            cc.SetCookies(new Uri(nextUrl), ck);
            html = WebOpHelper.GetHtml(nextUrl, accept, contentType, refer, language, cc, DefaultEncoding, out returnUrl);
            string addressUrl = "http://cn.address.mail.yahoo.com/";
            html = WebOpHelper.GetHtml(addressUrl, accept, contentType, refer, language, cc, Encoding.GetEncoding("utf-8"), out returnUrl);
            return YahooResover.Resove(html);
        }

        string Rewrite(string r, string d) {
            if(d.IndexOf("&.remember=") != -1 )
            { 
                d = d.Replace(@"/\.remember=(y|n)/i",".remember=" + r);
            }
            else{
                d += "&.remember="+r; 
            }
            return d;

        }

        string  Hash(string pwd, string challenge) {    
            var h1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "md5").ToLower();
            string h2 = string.Concat(h1, challenge);
            string h = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(h2, "md5").ToLower();
            return h;
        }
        #endregion
    }
}
